package com.nd.qa.shareplate.script.macaca;

import com.nd.qa.shareplate.common.GetElementWay;
import com.nd.qa.shareplate.common.SysConstant;
import com.nd.qa.shareplate.model.Module;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.model.Project;
import com.nd.qa.shareplate.model.ProjectType;
import com.nd.qa.shareplate.util.CharacterUtils;
import com.nd.qa.shareplate.util.StringUtil;

/**
 * 创建脚本类
 * @author huangjian
 *
 */
public class InitMacacaScript {

	public String initMacacaScript(Module module,ProjectType projectType,String scriptName,Project project){
		String platFormType = "Android";
		String initStr = "";
		String result = "";
		if (project.getPlatformType()==1) {
			platFormType = "Web";
			initStr = "package com.nd.qa.macaca;\n"
					+"import com.alibaba.fastjson.JSONObject;\n"
					+"import com.nd.qa.macaca.client.common.SysConstant;\n"
					+"import com.nd.qa.macaca.client.handle.HandleData;\n"
					+"import com.nd.qa.macaca.client.model.TestCases;\n"
					+"import com.nd.qa.macaca.client.model.TestClass;\n"
					+"import com.nd.qa.macaca.client.tools.BaseMacacaClient;\n"
					+"import com.nd.qa.macaca.client.tools.BaseMacacaClient.PlatformType;\n"
					+"import com.nd.qa.macaca.client.common.GetElementWay;\n"
					+"import com.nd.qa.macaca.client.utils.DateUtil;\n"
					+"import org.junit.After;\n"
					+"import org.junit.Before;\n"
					+"import org.junit.Test;\n"
					+"public class %s {\n"
					+"    	BaseMacacaClient driver = new BaseMacacaClient();\n"
					+"		HandleData handle = new HandleData();\n"
					+"		private int classId = 0;\n"
					+"		private int projectId = 0;\n"
					+"		private String deviceId = \"\";\n"
					+"@Before\n"
					+" public void setUp() throws Exception {\n"
					+"		JSONObject porps = new JSONObject();\n"
					+"		porps.put(\"browserName\", \"electron\");\n"
					+"		porps.put(\"platformName\", \"desktop\");\n"
					+"		driver.setCurPlatform(PlatformType.WEB);\n"
					+"		driver.setScreenShotPath(\"uScreenShotPath\");\n"
					+"		JSONObject desiredCapabilities = new JSONObject();\n"
					+"		desiredCapabilities.put(\"desiredCapabilities\", porps);\n"
					+"		desiredCapabilities.put(\"host\", \"uHost\");\n"
					+"		desiredCapabilities.put(\"port\", Integer.parseInt(\"uPort\"));\n"
					+"		driver.initDriver(desiredCapabilities);\n"
					+"		TestClass testClass = new TestClass();\n"
					+"		projectId = %s;\n"
					+"		deviceId = \"uDeviceId\";\n"
					+"		testClass.setClassName(Thread.currentThread().getStackTrace()[1].getClassName());\n"
					+"		testClass.setCreateTime(DateUtil.getSimpleDateFormat());\n"
					+"		testClass.setProjectId(projectId);\n"
					+"		classId = handle.insert(testClass);\n"
					+"}\n"
					+" @After\n"
					+"public void tearDown() throws Exception {\n"
					+"		TestClass testClass = new TestClass();\n"
					+"		testClass.setClassId(classId);\n"
					+"		testClass.setTotalCase(SysConstant.TOTAL_CASE_COUNT);\n"
					+"		testClass.setEndTime(DateUtil.getSimpleDateFormat());\n"
					+"		handle.update(testClass);\n"
					+"		driver.quit();\n"
					+"}\n";
			 result = String.format(initStr,scriptName,platFormType,project.getProjectPackage(),project.getProjectMainActivity(),project.getProjectId());
		}else if (project.getPlatformType()==2) {
			platFormType = "Android";
			initStr = "package com.nd.qa.macaca;\n"
					+"import com.alibaba.fastjson.JSONObject;\n"
					+"import com.nd.qa.macaca.client.common.SysConstant;\n"
					+"import com.nd.qa.macaca.client.handle.HandleData;\n"
					+"import com.nd.qa.macaca.client.model.TestCases;\n"
					+"import com.nd.qa.macaca.client.model.TestClass;\n"
					+"import com.nd.qa.macaca.client.tools.BaseMacacaClient;\n"
					+"import com.nd.qa.macaca.client.tools.BaseMacacaClient.PlatformType;\n"
					+"import com.nd.qa.macaca.client.common.GetElementWay;\n"
					+"import com.nd.qa.macaca.client.utils.DateUtil;\n"
					+"import org.junit.After;\n"
					+"import org.junit.Before;\n"
					+"import org.junit.Test;\n"
					+"public class %s {\n"
					+"    	BaseMacacaClient driver = new BaseMacacaClient();\n"
					+"		HandleData handle = new HandleData();\n"
					+"		private int classId = 0;\n"
					+"		private int projectId = 0;\n"
					+"		private String deviceId = \"\";\n"
					+"		private String deviceName = \"\";\n"
					+"		private String moduleName = \"%s\";\n"
					+"@Before\n"
					+" public void setUp() throws Exception {\n"
					+"		JSONObject porps = new JSONObject();\n"
					+" 		porps.put(\"autoAcceptAlerts\", true);\n"
					+"		porps.put(\"javascriptEnabled\", true);\n"
					+"		porps.put(\"deviceName\", \"uDeviceName\");\n"
					+"		porps.put(\"udid\", \"uDeviceId\");\n"
					+"		porps.put(\"platformName\", \"%s\");\n"
					+"		porps.put(\"platformVersion\", \"uPlatformVersion\");\n"
					+"		porps.put(\"version\", \"\");\n"
					+"		porps.put(\"package\", \"%s\");\n"
					+"		porps.put(\"activity\", \"%s\");\n"
					+"		porps.put(\"reuse\", 2);\n"
					+"		driver.setCurPlatform(PlatformType.ANDROID);\n"
					+"		driver.setScreenShotPath(\"uScreenShotPath\");\n"
					+"		JSONObject desiredCapabilities = new JSONObject();\n"
					+"		desiredCapabilities.put(\"desiredCapabilities\", porps);\n"
					+"		desiredCapabilities.put(\"host\", \"uHost\");\n"
					+"		desiredCapabilities.put(\"port\", Integer.parseInt(\"uPort\"));\n"
					+"		driver.initDriver(desiredCapabilities);\n"
					+"		TestClass testClass = new TestClass();\n"
					+"		projectId = %s;\n"
					+"		deviceId = \"uDeviceId\";\n"
					+"		deviceName = \"uDeviceName\";\n"
					+"		testClass.setModuleName(moduleName);\n"
					+"		testClass.setClassName(Thread.currentThread().getStackTrace()[1].getClassName());\n"
					+"		testClass.setCreateTime(DateUtil.getSimpleDateFormat());\n"
					+"		testClass.setProjectId(projectId);\n"
					+"		testClass.setDeviceId(deviceId);\n"
					+"		testClass.setDeviceName(deviceName);\n"
					+"		if (0==SysConstant.TEST_CLASS_ID) {\n"
					+"			classId = handle.insert(testClass);\n"
					+"			SysConstant.TEST_CLASS_ID = classId;\n"
					+"		}else{\n"
					+"			classId = SysConstant.TEST_CLASS_ID;\n"
					+"		}\n"
					+"}\n"
					+" @After\n"
					+"public void tearDown() throws Exception {\n"
					+"		driver.quit();\n"
					+"		TestClass testClass = new TestClass();\n"
					+"		testClass.setClassId(classId);\n"
					+"		testClass.setTotalCase(SysConstant.TOTAL_CASE_COUNT);\n"
					+"		testClass.setEndTime(DateUtil.getSimpleDateFormat());\n"
					+"		handle.update(testClass);\n"
					+"}\n"
					+"@Test\n"
					+"public void test_Alogin() throws Exception {\n"
						+"TestCases testCase = new TestCases();\n"
						+"SysConstant.TOTAL_CASE_COUNT++;\n"
						+"int stepId = 0;\n"
						+"String method = Thread.currentThread() .getStackTrace()[1].getMethodName();\n"
						+"testCase.setCaseName(method);\n"
						+"testCase.setClassId(classId);\n"
						+"testCase.setCreateTime(DateUtil.getSimpleDateFormat());\n"
						+"int caseId = handle.insert(testCase);\n"
						+"driver.scrollToLeft(driver, 6);\n"
						//+"stepId =driver.elementsByClassName(driver,\"android.widget.Button\",0,\"click\",\"\",\"登录导航页\",\"点击开始到登录界面\",caseId);\n";
						+"stepId =driver.elementsById(driver,\"iv_guide_go_on\",0,\"click\",\"\",\"登录导航页\",\"点击开始到登录界面\",caseId);\n";
				if (null!=projectType.getBusinessId()&&!"".equals(projectType.getBusinessId())) {
					initStr = initStr+ "stepId =driver.elementsByClassName(driver,\"android.widget.EditText\",0,\"sendKey\",\""+projectType.getBusinessId()+"\",\"登录页\",\"企业账号\",caseId);\n"
							+"stepId =driver.elementsByClassName(driver,\"android.widget.EditText\",1,\"sendKey\",\"uUserName\",\"登录页\",\"输入账号\",caseId);\n"
							+"stepId =driver.elementsByClassName(driver,\"android.widget.EditText\",2,\"sendKey\",\"uPassword\",\"登录页\",\"输入密码\",caseId);\n";
				}else{
					initStr = initStr+ "stepId =driver.elementsByClassName(driver,\"android.widget.EditText\",0,\"sendKey\",\"uUserName\",\"登录页\",\"输入账号\",caseId);\n"
							+"stepId =driver.elementsByClassName(driver,\"android.widget.EditText\",1,\"sendKey\",\"uPassword\",\"登录页\",\"输入密码\",caseId);\n";
				}
				initStr	= initStr+"stepId =driver.elementsByClassName(driver,\"android.widget.Button\",0,\"click\",\"\",\"登录页\",\"点击登录\",caseId);\n"
						+"testCase.setCaseId(caseId);\n"
						+"testCase.setStepCount(SysConstant.TOTAL_STEP_COUNT);\n"
						+"testCase.setCaseErrors(SysConstant.FAIL_STEP_COUNT);\n"
						+"testCase.setCaseFailures(SysConstant.FAIL_STEP_COUNT);\n"
						+"testCase.setEndTime(DateUtil.getSimpleDateFormat());\n"
						+"testCase.setSpendTime(DateUtil.getDiffTime(testCase.getEndTime(),testCase.getCreateTime()));\n"
						+"testCase.setSuccessRate((double)(SysConstant.TOTAL_STEP_COUNT-SysConstant.FAIL_STEP_COUNT)/(SysConstant.TOTAL_STEP_COUNT));\n"
						+"handle.update(testCase);\n"
						+"SysConstant.TOTAL_STEP_COUNT=0;\n"
						+"SysConstant.FAIL_STEP_COUNT=0;\n"
						+"}\n";
			 result = String.format(initStr,scriptName,module.getModuleName(),platFormType,project.getProjectPackage(),project.getProjectMainActivity(),project.getProjectId());
		}else {
			platFormType = "ios";
			initStr = "package com.nd.qa.macaca;\n"
					+"import com.alibaba.fastjson.JSONObject;\n"
					+"import com.nd.qa.macaca.client.common.SysConstant;\n"
					+"import com.nd.qa.macaca.client.handle.HandleData;\n"
					+"import com.nd.qa.macaca.client.model.TestCases;\n"
					+"import com.nd.qa.macaca.client.model.TestClass;\n"
					+"import com.nd.qa.macaca.client.tools.BaseMacacaClient;\n"
					+"import com.nd.qa.macaca.client.tools.BaseMacacaClient.PlatformType;\n"
					+"import com.nd.qa.macaca.client.common.GetElementWay;\n"
					+"import com.nd.qa.macaca.client.utils.DateUtil;\n"
					+"import org.junit.After;\n"
					+"import org.junit.Before;\n"
					+"import org.junit.Test;\n"
					+"public class %s {\n"
					+"		BaseMacacaClient driver = new BaseMacacaClient();\n"
					+"		HandleData handle = new HandleData();\n"
					+"		private int classId = 0;\n"
					+"		private int projectId = 0;\n"
					+"		private String deviceId = \"\";\n"
					+"		private String deviceName = \"\";\n"
					+"		private String moduleName = \"%s\";;\n"
					+"@Before\n"
					+" public void setUp() throws Exception {\n"
					+"		JSONObject porps = new JSONObject();\n"
					+" 		porps.put(\"autoAcceptAlerts\", true);\n"
					+"		porps.put(\"javascriptEnabled\", true);\n"
					+"		porps.put(\"deviceName\", \"uDeviceName\");\n"
					+"		porps.put(\"bundleId\", \"uBundleId\");\n"
					+"		porps.put(\"udid\", \"uDeviceId\");\n"
					+"		porps.put(\"platformName\", \"%s\");\n"
					+"		porps.put(\"platformVersion\", \"uPlatformVersion\");\n"
					+"		porps.put(\"version\", \"\");\n"
					+"		porps.put(\"reuse\", 2);\n"
					+"		driver.setCurPlatform(PlatformType.IOS);\n"
					+"		driver.setScreenShotPath(\"uScreenShotPath\");\n"
					+"		JSONObject desiredCapabilities = new JSONObject();\n"
					+"		desiredCapabilities.put(\"host\", \"uHost\");\n"
					+"		desiredCapabilities.put(\"port\", Integer.parseInt(\"uPort\"));\n"
					+"		desiredCapabilities.put(\"desiredCapabilities\", porps);\n"
					+"		driver.initDriver(desiredCapabilities);\n"
					+"		TestClass testClass = new TestClass();\n"
					+"		projectId = %s;\n"
					+"		deviceId = \"uDeviceId\";\n"
					+"		deviceName = \"uDeviceName\";\n"
					+"		testClass.setModuleName(moduleName);\n"
					+"		testClass.setClassName(Thread.currentThread().getStackTrace()[1].getClassName());\n"
					+"		testClass.setCreateTime(DateUtil.getSimpleDateFormat());\n"
					+"		testClass.setProjectId(projectId);\n"
					+"		testClass.setDeviceId(deviceId);\n"
					+"		testClass.setDeviceName(deviceName);\n"
					+"		if (0==SysConstant.TEST_CLASS_ID) {\n"
					+"			classId = handle.insert(testClass);\n"
					+"			SysConstant.TEST_CLASS_ID = classId;\n"
					+"		}else{\n"
					+"			classId = SysConstant.TEST_CLASS_ID;\n"
					+"		}\n"
					+"}\n"
					+" @After\n"
					+"public void tearDown() throws Exception {\n"
					+"		TestClass testClass = new TestClass();\n"
					+"		testClass.setClassId(classId);\n"
					+"		testClass.setTotalCase(SysConstant.TOTAL_CASE_COUNT);\n"
					+"		testClass.setEndTime(DateUtil.getSimpleDateFormat());\n"
					+"		handle.update(testClass);\n"
					+"		driver.quit();\n"
					+"}\n"
					+"@Test\n"
					+"public void test_Alogin() throws Exception {\n"
						+"TestCases testCase = new TestCases();\n"
						+"SysConstant.TOTAL_CASE_COUNT++;\n"
						+"int stepId = 0;\n"
						+"String method = Thread.currentThread() .getStackTrace()[1].getMethodName();\n"
						+"testCase.setCaseName(method);\n"
						+"testCase.setClassId(classId);\n"
						+"testCase.setCreateTime(DateUtil.getSimpleDateFormat());\n"
						+"int caseId = handle.insert(testCase);\n"
						+"driver.scrollToLeft(driver, 6);\n"
						+"stepId =driver.elementsByClassName(driver,\"Button\",0,\"click\",\"\",\"登录导航页\",\"点击开始到登录界面\",caseId);\n";
			if (null!=projectType.getBusinessId()&&!"".equals(projectType.getBusinessId())) {
				initStr = initStr+ "stepId =driver.elementsByClassName(driver,\"TextField \",0,\"sendKey\",\""+projectType.getBusinessId()+"\",\"登录页\",\"企业账号\",caseId);\n"
						+"stepId =driver.elementsByClassName(driver,\"TextField\",1,\"sendKey\",\"uUserName\",\"登录页\",\"输入账号\",caseId);\n"
						+"stepId =driver.elementsByClassName(driver,\"SecureTextField \",0,\"sendKey\",\"uPassword\",\"登录页\",\"输入密码\",caseId);\n";
			}else{
				initStr = initStr+ "stepId =driver.elementsByClassName(driver,\"TextField \",0,\"sendKey\",\"uUserName\",\"登录页\",\"输入账号\",caseId);\n"
						+"stepId =driver.elementsByClassName(driver,\"SecureTextField \",0,\"sendKey\",\"uPassword\",\"登录页\",\"输入密码\",caseId);\n";
			}
			initStr	= initStr+"stepId =driver.elementsByClassName(driver,\"Button\",0,\"click\",\"\",\"登录页\",\"点击登录\",caseId);\n"
					+"testCase.setCaseId(caseId);\n"
					+"testCase.setStepCount(SysConstant.TOTAL_STEP_COUNT);\n"
					+"testCase.setCaseErrors(SysConstant.FAIL_STEP_COUNT);\n"
					+"testCase.setCaseFailures(SysConstant.FAIL_STEP_COUNT);\n"
					+"testCase.setEndTime(DateUtil.getSimpleDateFormat());\n"
					+"testCase.setSpendTime(DateUtil.getDiffTime(testCase.getEndTime(),testCase.getCreateTime()));\n"
					+"testCase.setSuccessRate((double)(SysConstant.TOTAL_STEP_COUNT-SysConstant.FAIL_STEP_COUNT)/(SysConstant.TOTAL_STEP_COUNT));\n"
					+"handle.update(testCase);\n"
					+"SysConstant.TOTAL_STEP_COUNT=0;\n"
					+"SysConstant.FAIL_STEP_COUNT=0;\n"
					+"}\n";
			 result = String.format(initStr,scriptName,module.getModuleName(),platFormType,project.getProjectId(),projectType.getProjectUserName(),projectType.getProjectUserPwd());
		}	
		return result;
	}
	
	/**
	 * 点击元素操作
	 * @param element
	 * @return
	 */
	public String click(PageElement element){
		String elementTypeName = element.getElementTypeName();
		String code = "";
		if (SysConstant.ELEMENT_TYPE_ID.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsById(driver,\"%s\",%s,\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementById(driver,\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_NAME.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByName(driver,\"%s\",\"%s\",%s,\"\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByName(driver,\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_XPATH.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByXPath(driver,\"%s\",\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByXPath(driver,\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_XPATH.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByXPath(driver,\"%s\",\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByXPath(driver,\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_CLASSNAME.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByClassName(driver,\"%s\",\"%s\",%s,\"\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByClassName(driver,\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_CSSSELECTOR.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByCss(driver,\"%s\",\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByCss(driver,\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_TAGNAME.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByTagName(driver,\"%s\",\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByTagName(driver,\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_LINKTEXT.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByLinkText(driver,\"%s\",\"%s\",\"%s\",\"\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByLinkText(driver,\"%s\",\"%s\",\"\",\"%s\",\"%s\");\n";
			}
		}
		//增加校验的功能 checkHandleResult(MacacaClient mDriver,String by,String byParam,int index,String type,String expectResult,int stepId){
		if (!StringUtil.isEmptyString(element.getElementExpectResult())&&!StringUtil.isEmptyString(element.getElementExpectContent())) {
			code = code+ "driver.checkHandleResult(driver,\""+element.getElementExpectName()+"\",\""+element.getElementExpectContent()+"\","+element.getElementExpectOrder()+",\""+element.getElementExpectType()+"\",\""+element.getElementExpectResult()+"\",stepId);\n";
		}
		if (element.getElementOrder()>0) {
			return String.format(code,element.getElementContent(),element.getElementOrder(),"click",element.getPageName(),element.getElementOperateDesc());
		}else{
			return String.format(code,element.getElementContent(),"click",element.getPageName(),element.getElementOperateDesc());
		}
	}
	/**
	 * 坐标点击操作
	 * @param element
	 * @return
	 */
	public String clickPoint(PageElement element){
		//解析element.getElementContent()
		String[] xyStr = element.getElementContent().split(",");
		String code = "stepId =driver.elementsByLinkText(driver,\"%s\",%s,%s,\"%s\",\"%s\",\"%s\",caseId);\n";
		return String.format(code,"click",Integer.valueOf(xyStr[0]),Integer.valueOf(xyStr[1]),element.getPageName(),element.getElementOperateDesc());
	}
	/**
	 * 验证操作结果方法
	 * @param element
	 * @return
	 */
	public String assertElement(PageElement element){
		String elementExpectName = element.getElementExpectName();
		String code = "";
		if (elementExpectName.equals(GetElementWay.ID.toString())) {
			code = "driver.assertIOSElementExist(GetElementWay.ID, \"%s\",%s, \"验证是否存在ID信息\", stepId);\n";
		}else if(elementExpectName.equals(GetElementWay.CSS.toString())){
			code = "driver.assertIOSElementExist(GetElementWay.CSS, \"%s\",%s, \"验证是否存在CSS信息\", stepId);\n";
		}else if(elementExpectName.equals(GetElementWay.NAME.toString())){
			code = "driver.assertIOSElementExist(GetElementWay.NAME, \"%s\",%s, \"验证是否存在NAME信息\", stepId);\n";
		}else if(elementExpectName.equals(GetElementWay.XPATH.toString())){
			code = "driver.assertIOSElementExist(GetElementWay.XPATH, \"%s\",%s, \"验证是否存在XPATH信息\", stepId);\n";
		}else if(elementExpectName.equals(GetElementWay.CLASS_NAME.toString())){
			code = "driver.assertIOSElementExist(GetElementWay.CLASS_NAME, \"%s\",%s, \"验证是否存在CLASS_NAME信息\", stepId);\n";
		}else if(elementExpectName.equals(GetElementWay.LINK_TEXT.toString())){
			code = "driver.assertIOSElementExist(GetElementWay.LINK_TEXT, \"%s\",%s, \"验证是否存在LINK_TEXT信息\", stepId);\n";
		}else if(elementExpectName.equals(GetElementWay.PARTIAL_LINK_TEXT.toString())){
			code = "driver.assertIOSElementExist(GetElementWay.PARTIAL_LINK_TEXT, \"%s\",%s, \"验证是否存在PARTIAL_LINK_TEXT信息\", stepId);\n";
		}else if(elementExpectName.equals(GetElementWay.TAG_NAME.toString())){
			code = "driver.assertIOSElementExist(GetElementWay.TAG_NAME, \"%s\",%s, \"验证是否存在TAG_NAME信息\", stepId);\n";
		}
		if (element.getElementExpectOrder()>0) {
			return String.format(code,element.getElementExpectResult(),element.getElementExpectOrder());
		}else{
			return String.format(code,element.getElementExpectResult(),0);
		}
	}
	public String longClick(PageElement element){
		
		return "";
	}
	/**
	 * 输入框操作
	 * @param element
	 * @return
	 */
	public String sendKey(PageElement element){
		String elementTypeName = element.getElementTypeName();
		String elementText = element.getElementText();
		if ("Y".equals(element.getElementTextFlag())) {
			elementText = elementText+CharacterUtils.getRandomString2(4);
		}
		String code = "";
		if (SysConstant.ELEMENT_TYPE_ID.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsById(driver,\"%s\",\"%s\",%s,\"%s\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementById(driver,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_NAME.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByName(driver,\"%s\",\"%s\",%s,\"%s\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByName(driver,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_XPATH.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByXPath(driver,\"%s\",\"%s\",%s,\"%s\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByXPath(driver,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_XPATH.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByXPath(driver,\"%s\",\"%s\",%s,\"%s\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByXPath(driver,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_CLASSNAME.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByClassName(driver,\"%s\",\"%s\",%s,\"%s\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByClassNam(driver,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_CSSSELECTOR.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByCss(driver,\"%s\",\"%s\",%s,\"%s\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByCss(driver,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_TAGNAME.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByTagName(driver,\"%s\",\"%s\",%s,\"%s\",\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByTagName(driver,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",caseId);\n";
			}
		}else if (SysConstant.ELEMENT_TYPE_LINKTEXT.equals(elementTypeName)) {
			if (element.getElementOrder()!=null&&element.getElementOrder()>0) {
				code = "stepId =driver.elementsByLinkText(driver,\"%s\",\"%s\",\"%s\",%s,\"%s\",\"%s\",caseId);\n";
			}else{
				code = "stepId =driver.elementByLinkText(driver,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",caseId);\n";
			}
		}
		//增加校验的功能 checkHandleResult(MacacaClient mDriver,String by,String byParam,int index,String type,String expectResult,int stepId){
		if (!StringUtil.isEmptyString(element.getElementExpectResult())&&!StringUtil.isEmptyString(element.getElementExpectContent())) {
			code = code+ "driver.checkHandleResult(driver,\""+element.getElementExpectName()+"\",\""+element.getElementExpectContent()+"\","+element.getElementExpectOrder()+",\""+element.getElementExpectType()+"\",\""+element.getElementExpectResult()+"\",stepId);\n";
		}
		if (element.getElementOrder()>0) {
			return String.format(code,element.getElementContent(),"sendKey",elementText,element.getElementOrder(),element.getPageName(),element.getElementOperateDesc());
		}else{
			return String.format(code,element.getElementContent(),"sendKey",elementText,element.getPageName(),element.getElementOperateDesc());
		}
	}
	/**
	 * 拖曳页面（上下左右）
	 * @param element
	 * @return
	 */
	public String scroll(PageElement element){
		String code = "";
		if (SysConstant.SCROLLUPQUERYBYNAME.equals(element.getElementActionName())) {
			code = "driver.scrollUpQueryByName(driver,\"%s\");\n";
		}else if(SysConstant.SCROLLUPQUERYBYID.equals(element.getElementActionName())){
			code = "driver.scrollUpQueryById(driver,\"%s\");\n";
		}else if(SysConstant.SCROLLDOWNQUERYBYNAME.equals(element.getElementActionName())){
			code = "driver.scrollDownQueryByName(driver,\"%s\");\n";
		}else if(SysConstant.SCROLLDOWNQUERYBYID.equals(element.getElementActionName())){
			code = "driver.scrollDownQueryById(driver,\"%s\");\n";
		}else if(SysConstant.SCROLLTOUP.equals(element.getElementActionName())){
			code = "driver.scrollToUp(driver,%s);\n";
		}else if(SysConstant.SCROLLTOUPONLEFT.equals(element.getElementActionName())){
			code = "driver.scrollToUpOnLeft(driver,%s);\n";
		}else if(SysConstant.SCROLLTOUPONRIGHT.equals(element.getElementActionName())){
			code = "driver.scrollToUpOnRight(driver,%s);\n";
		}else if(SysConstant.SCROLLTODOWN.equals(element.getElementActionName())){
			code = "driver.scrollToDown(driver,%s);\n";
		}else if(SysConstant.SCROLLTOLEFT.equals(element.getElementActionName())){
			code = "driver.scrollToLeft(driver,%s);\n";
		}else if (SysConstant.SCROLLTOLEFTONCENTER.equals(element.getElementActionName())) {
			code = "driver.scrollToLeftOnCenter(driver,%s);\n";
		}else if (SysConstant.SCROLLTOLEFTONBOTTOM.equals(element.getElementActionName())) {
			code = "driver.scrollToLeftOnBottom(driver,%s);\n";
		}else {
			code = "driver.scrollToRight(driver,%s);\n";
		}
		//增加校验的功能 checkHandleResult(MacacaClient mDriver,String by,String byParam,int index,String type,String expectResult,int stepId){
		if (!StringUtil.isEmptyString(element.getElementExpectResult())&&!StringUtil.isEmptyString(element.getElementExpectContent())) {
			code = code+ "driver.checkHandleResult(driver,\""+element.getElementExpectName()+"\",\""+element.getElementExpectContent()+"\","+element.getElementExpectOrder()+",\""+element.getElementExpectType()+"\",\""+element.getElementExpectResult()+"\",stepId);\n";
		}
		return String.format(code,element.getDragCount());
	}
	/**
	 * 模糊查找
	 * @param element
	 * @return
	 */
	public String query(PageElement element){
		String code = "";
		if (element.getElementExpectResult()!=null) {
			code = code+ "driver.fuzzyQuery(driver,\""+element.getElementExpectResult()+"\",stepId);\n";
		}
		return code;
	}
	/**
	 * 查找元素信息不存在
	 * @param element
	 * @return
	 */
	public String queryNotExist(PageElement element){
		String code = "";
		if (element.getElementExpectName()!=null&&element.getElementExpectContent()!=null) {
			code = code+ "driver.queryNotExist(driver,\""+element.getElementExpectName()+"\",\""+element.getElementExpectContent()+"\",\""+element.getElementExpectOrder()+"\",stepId);\n";
		}
		return code;
	}
	/**
	 * 返回
	 * @param element
	 * @return
	 */
	public String back(PageElement element){
		String	code = "driver.goBack(driver);\n";
		return code;
	}
	/**
	 * 操作物理键
	 * @param element
	 * @return
	 */
	public String keys(PageElement element){
		String	code = "driver.keys(\"%s\");\n";
		return String.format(code,element.getElementContent());
	}
	public String queryByKeyBoard(PageElement element){
		
		return "";
	}
	/**
	 * 根据某一个元素位置拖曳
	 * @param element
	 * @return
	 */
	public String dragByView(PageElement element){
		
		return "";
	}
}
